package com.group3.customer.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group3.customer.service.ICustomerDishService;
import com.group3.customer.service.ICustomerMerchantService;
import com.group3.customer.service.IDishTypeService;
import com.group3.customer.service.impl.CustomerDishServiceImpl;
import com.group3.customer.service.impl.CustomerMerchantServiceImpl;
import com.group3.customer.service.impl.DishTypeServiceImpl;
import com.group3.customer.util.SenderUtil;
import com.group3.entity.Customer;
import com.group3.entity.Dish;
import com.group3.entity.DishType;
import com.group3.entity.Merchant;

/**
 * Servlet implementation class DishListServlet
 */
public class DishListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ICustomerMerchantService merchanService = new CustomerMerchantServiceImpl();
	private ICustomerDishService dishService = new CustomerDishServiceImpl();
	private IDishTypeService dishTypeService = new DishTypeServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DishListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		dishList(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		dishList(request, response);
	}
	
	private void dishList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		List<DishType> dishTypes = dishTypeService.getDishTypeList();
		map.put("dishtype", dishTypes);
		String mId = request.getParameter("mId");
		Merchant merchant = merchanService.queryMercantByMId(mId);
		String type = request.getParameter("type"); 
//		request.setAttribute("merchant", merchant);
		map.put("merchant", merchant);
//		System.out.println(mId + "..." + type);
		HttpSession session=request.getSession();
//		HttpSession session = request.getSession();
//		session.setAttribute("defaultType", type);
		List<Dish> dishes = null;
		if(type == null || "000".equals(type)) {
			dishes = dishService.queryDishListByMid(mId);
		} else {
			dishes = dishService.queryDishListByMidAndType(mId, type);
		}
//		System.out.println("DishListServlet Dishes: " + dishes);
		map.put("dishes", dishes);
		Customer user=(Customer) session.getAttribute("loginToken");
		map.put("loginToken", user);
		
		SenderUtil.send(map, response);
//		request.setAttribute("dishes", dishes);
//		request.getRequestDispatcher("listdish.jsp").forward(request, response);
	}
}
